home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Demos / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Analysis / DSP (Fourier etc) / Power Spectral Density < prev   
Encoding:
Text File  |  1994-11-14  |  2.8 KB  |  91 lines  |  [TEXT/IGR0]

  1.  
  2. #include <DSP Window Functions>
  3. #include <BringDestToFront>
  4.  
  5. | Given a long data wave create a short result wave containing the Power
  6. | Spectral Density.  The name of the new wave is the name of the source + _psd
  7. | Note: if you don't want the baggage of all the window functions, create your own version
  8. | with your favorite and remove the window parameter (or choose a subset)
  9. | Also, note that a unity amplitude sine wave will give an integral peak size of
  10. | 0.5 rather than 0.707 as one would get if a real sine wave of 1 Volt peak amplitude
  11. | were applied across a 1 ohm resistor.  Thus if you want physical meaning for
  12. | the results you should scale by 1.414/R where R is the actual resistance.
  13. |
  14. |    Change 941114: Divided DC component by 2
  15. Macro PSD(w,seglen,window)
  16.     string w
  17.     Prompt w "data wave:",popup WaveList("*",";","")
  18.     variable seglen=1
  19.     Prompt seglen,"segment length:",popup "256;512;1024;2048;4096;8192"
  20.     variable window=2
  21.     Prompt window,"Window type:",popup "Square;Hann;Parzen;Welch;Hamming;"
  22.                         "BlackmanHarris3;KaiserBessel"
  23. ;
  24.     PauseUpdate; silent 1
  25.     
  26.     variable npsd= 2^(7+seglen)                | number of points in group (resultant psd wave len= npsd/2+1)
  27.     variable psdOffset= npsd/2                    | offset each group by this amount
  28.     variable psdFirst=0                            | start of current group
  29.     variable nsrc= numpnts($w)
  30.     variable nsegs,winNorm                        | count of number of segements and window normalization factor
  31.     string destw=w+"_psd",srctmp=w+"_tmp"
  32.     string winw=w+"_psdWin"                    | window goes here
  33.     
  34.     if( npsd > nsrc/2 )
  35.         Abort "psd: source wave should be MUCH longer than the segment length"
  36.     endif
  37.     make/o/n=(npsd/2+1) $destw
  38.     make/o/n=(npsd) $srctmp,$winw; $winw= 1
  39.     if( window==1 )
  40.         winNorm= 1
  41.     else
  42.         if( window==2 )
  43.             Hanning $winw;winNorm=0.372                |  winNorm is avg squared value
  44.         else
  45.             if( window==3 )
  46.                 winNorm= Parzen($winw)
  47.             else
  48.                 if( window==4 )
  49.                     winNorm= Welch($winw)
  50.                 else
  51.                     if( window==5 )
  52.                         winNorm= Hamming($winw)
  53.                     else
  54.                         if( window==6 )
  55.                             winNorm= BlackmanHarris3($winw)
  56.                         else
  57.                             if( window==7 )
  58.                                 winNorm= KaiserBessel($winw)
  59.                             else
  60.                                 Abort "unknown window index"
  61.                             endif
  62.                         endif
  63.                     endif
  64.                 endif
  65.             endif
  66.         endif
  67.     endif    | (kinda makes you wish we had elseif or switch constructs, huh?)
  68.  
  69.     Duplicate/O/R=[0,npsd-1] $w $srctmp; $srctmp *= $winw; fft $srctmp
  70.     CopyScales/P $srctmp, $destw
  71.     $destw= magsqr($srctmp)
  72.     psdFirst= psdOffset
  73.     nsegs=1
  74.     do
  75.         Duplicate/O/R=[psdFirst,psdFirst+npsd-1] $w $srctmp;   $srctmp *= $winw
  76.         fft $srctmp;   $destw += magsqr($srctmp);   psdFirst += psdOffset; nsegs+=1
  77.     while( psdFirst+npsd < nsrc )
  78.     winNorm= 2/(winNorm*nsegs*npsd^2);  $destw *= winNorm
  79.     $destw[0] /= 2
  80.  
  81.     KillWaves $srctmp,$winw
  82.     BringDestFront(destw)
  83.     if( numpnts($destw) <= 129 )
  84.         Modify mode($destw)=4,marker($destw)=19,msize($destw)=1
  85.     else
  86.         Modify mode($destw)=0
  87.     endif
  88. end
  89.  
  90.